package com.ruike.alisurface.ui.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.bean.ShoptypeBean;
import com.wanjian.view.ExpandableAdapter;

import java.util.List;


public class ShopModelAdapter extends ExpandableAdapter<GroupHolder, ChildHolder> {

    List<ShoptypeBean.DataBean> dataBeanList;


    public ShopModelAdapter(List<ShoptypeBean.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
    }

    @Override
    protected int getGroupCount() {
        return dataBeanList.size();
    }

    @Override
    protected int getChildCount(int groupIndex) {
        return dataBeanList.get(groupIndex).getChildren().size();
    }

    @Override
    protected GroupHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {

        Context context = parent.getContext();

        return new GroupHolder(LayoutInflater.from(context).inflate(R.layout.main_left_list_item, parent, false));


    }

    @Override
    protected void onBindGroupViewHolder(GroupHolder groupHolder, int i) {
        groupHolder.tvTitle.setText(dataBeanList.get(i).getLabel());

        groupHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("shopModelAdapter=", "isExpanded(i)   -====     " + isExpanded(i));

                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(i, dataBeanList.get(i));
                }
            }
        });
        if (!isExpanded(i)) {
            groupHolder.image.setImageResource(R.drawable.leftlist_item_right_sanjiao);
        } else {
            groupHolder.image.setImageResource(R.drawable.leftlist_item_down_sanjiao);
        }
    }

    @Override
    protected ChildHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        return new ChildHolder(LayoutInflater.from(context).inflate(R.layout.main_left_list_item_child, parent, false));

    }

    @Override
    protected void onBindChildViewHolder(ChildHolder childHolder, int i, int i1) {
//        Log.i("ShopModelAdapter=", "onBindChildViewHolder  " + childrenBeanX);
        Log.i("shopModelAdapter=", "onItemChildClick 组id ==" + i + "子id==" + i1);

        ShoptypeBean.DataBean.ChildrenBeanX childrenBeanX = dataBeanList.get(i).getChildren().get(i1);
        Log.i("shopModelAdapter=", "onItemChildClick  " + childrenBeanX);

        childHolder.tvTitle.setText(childrenBeanX.getLabel());

        if (childrenBeanX.isSelect()) {

            childHolder.image.setImageResource(R.drawable.icon_item_xz);
        } else {
            childHolder.image.setImageResource(R.drawable.icon_item_wxz);
            childHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {

                        onItemClickListener.onItemChildClick(i, i1, dataBeanList.get(i).getChildren().get(i1));
                    }
                }
            });
        }
    }

    public void setChildUpdataAdapter(int groupposition, int position, ShoptypeBean.DataBean.ChildrenBeanX data) {
        setChildUpdata();
        Log.i("shopModelAdapter111=", "onItemChildClick 组id ==" + groupposition + "子id==" + position);
        dataBeanList.get(groupposition).getChildren().get(position).setSelect(true);
        notifyDataSetChanged();
    }

    public void setChildUpdata() {
        for (int i = 0; i < dataBeanList.size(); i++) {
            for (int j = 0; j < dataBeanList.get(i).getChildren().size(); j++) {
                dataBeanList.get(i).getChildren().get(j).setSelect(false);
            }
        }
    }

    protected OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, ShoptypeBean.DataBean data);

        void onItemChildClick(int groupposition, int position, ShoptypeBean.DataBean.ChildrenBeanX data);
    }
}

class GroupHolder extends RecyclerView.ViewHolder {

    public ImageView image;
    public TextView tvTitle;

    /**
     * 初始化你的View(这里包括GroupView,和childView)
     */
    public GroupHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.left_item_isExand_img);
        tvTitle = (TextView) itemView.findViewById(R.id.left_list_title_label);

    }


}

class ChildHolder extends RecyclerView.ViewHolder {

    public ImageView image;
    public TextView tvTitle;

    /**
     * 初始化你的View(这里包括GroupView,和childView)
     */
    public ChildHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.left_item_isExand_img);
        tvTitle = (TextView) itemView.findViewById(R.id.left_list_title_label);
    }
}

